package com.hzcx.app.simplechat.ui.mine;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseFragment;
import com.hzcx.app.simplechat.ui.mine.contract.MineContract;
import com.hzcx.app.simplechat.ui.mine.event.EditUserInfoSuccesEvent;
import com.hzcx.app.simplechat.ui.mine.event.RefreshUserEvent;
import com.hzcx.app.simplechat.ui.mine.presenter.MinePresenter;
import com.hzcx.app.simplechat.ui.moment.NewMessageActivity;
import com.hzcx.app.simplechat.ui.publicui.QrCodeActivity;
import com.hzcx.app.simplechat.ui.publicui.ShareActivity;
import com.hzcx.app.simplechat.ui.setting.FeedBackActivity;
import com.hzcx.app.simplechat.ui.setting.SettingActivity;
import com.hzcx.app.simplechat.ui.setting.VipActivity;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.view.imageview.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的
 */
public class MineFragment extends BaseFragment<MinePresenter> implements MineContract.View {
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.iv_head)
    RoundImageView ivHead;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;
    @BindView(R.id.view_top_bg)
    View viewTopBg;
    @BindView(R.id.tv_mine_msg)
    TextView tvMineMsg;
    @BindView(R.id.tv_mine_share)
    TextView tvMineShare;
    @BindView(R.id.tv_mine_vip)
    TextView tvMineVip;
    @BindView(R.id.tv_mine_feedback)
    TextView tvMineFeedback;
    @BindView(R.id.tv_mine_setting)
    TextView tvMineSetting;

    @Override
    public int getLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initPresenter() {
        mPresenter = new MinePresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mPresenter.getUserInfo(getActivity());
        if (UserInfoUtil.getUserSign() != null) {
            GlideUtils.loadImgAsCenter(getActivity(), UserInfoUtil.getUserHead(), ivHead);
            tvNickName.setText(UserInfoUtil.getUserNickName());
            tvUserId.setText(getResources().getString(R.string.app_name) + "号：" + UserInfoUtil.getUserNumber());
        }
        tvMineVip.setText(getResources().getString(R.string.app_name) + "专业版");
    }

    @OnClick({R.id.tv_mine_setting, R.id.view_top_bg, R.id.tv_mine_share, R.id.tv_mine_feedback, R.id.tv_mine_msg, R.id.iv_qrcode,
            R.id.tv_mine_vip})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mine_setting: // 设置
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;

            case R.id.view_top_bg: // 个人信息及动态列表页
                startActivity(new Intent(getActivity(), MineUserInfoActivity.class));
                break;

            case R.id.tv_mine_share: // 分享
                startActivity(new Intent(getActivity(), ShareActivity.class)
                        .putExtra(ShareActivity.INTENT_USER_ID, UserInfoUtil.getUserId()));
                break;

            case R.id.tv_mine_feedback: // 意见反馈
                startActivity(new Intent(getActivity(), FeedBackActivity.class));
                break;

            case R.id.tv_mine_msg: // 我的消息
                startActivity(new Intent(getActivity(), NewMessageActivity.class));
                break;

            case R.id.iv_qrcode: // 二维码
                startActivity(new Intent(getActivity(), QrCodeActivity.class));
                break;

            case R.id.tv_mine_vip: // 多聊专业版
                startActivity(new Intent(getActivity(), VipActivity.class));
                break;
        }
    }

    /**
     * 用户信息获取成功
     *
     * @param data
     */
    @Override
    public void userInfoResult(UserInfoBean data) {
        GlideUtils.loadImgAsCenter(getActivity(), data.getAvatar(), ivHead);
        tvNickName.setText(data.getNickname());
        tvUserId.setText(getResources().getString(R.string.app_name) + "号：" + data.getChatnumber());
        UserInfoUtil.saveUser(data);
        EventBus.getDefault().post(new RefreshUserEvent());
    }

    /**
     * 用户信息修改成功，刷新UI
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void editUserInfoSuccess(EditUserInfoSuccesEvent event) {
        mPresenter.getUserInfo(getActivity());
    }
}
